package com.java.base.io.enter;

import java.io.Console;
import java.util.Scanner;

public abstract class EnterLine {

    public static void main(String[] args) {
        EnterLine.get(null).readLine();
    }


     String head;

    private EnterLine(String head) {
        this.head = head;
    }

    public String getHead() {
        return head;
    }

    public abstract String readLine();

    public static EnterLine get(String head) {
        if (head == null) {
            head = ">";
        }
        if (System.console() != null) {
            return new ConsoleEnterLine(head);
        }

        return new SystemInEnterLine(head);
    }

    public void printMsg(String msg) {
        System.out.print("\r" + msg + "\n" + head);
    }


    public static class SystemInEnterLine extends EnterLine {
        Scanner scanner = new Scanner(System.in);

        public SystemInEnterLine(String head) {
            super(head);
        }

        @Override
        public String readLine() {
            System.out.print(head);
            return scanner.nextLine();
        }
    }


    public static class ConsoleEnterLine extends EnterLine {
        Console console = System.console();

        public ConsoleEnterLine(String head) {
            super(head);
        }

        @Override
        public String readLine() {
            System.out.print(head);
            return console.readLine(head);
        }

    }

}

